#include<bits/stdc++.h>
using namespace std;
map<string, vector<string>> adjlist;
map<string, int> indeg;
void solve() {
  int n,m;cin>>n;
  string A, B;
  for(int i=0; i<n; ++i) {
    cin>>A;
    adjlist[A] = vector<string>(0);
    indeg[A] = 0;
  }
  cin>>m;
  for(int i=0; i<m; ++i) {
    cin>>A>>B;
    adjlist[A].push_back(B);
  }

  for(auto tmp:adjlist) {
    auto s = tmp.first;
    auto v = tmp.second;
    for(auto el:v) {
      indeg[el]++;
    }
  }
  priority_queue<string,vector<string>,greater<string>> q;
  vector<string> topo;
  for(auto tmp:indeg) {
    auto s = tmp.first;
    auto i = tmp.second;
    if(i==0) {
      q.push(s);
      break;
    }
  }
  while(!q.empty()) {
    auto u = q.top();q.pop();
    topo.push_back(u);
    for(auto el:adjlist[u]) {
      if(--indeg[el] == 0) {
        q.push(el);
      }
    }
  }
  for(auto el:topo) {
    cout<<' '<<el;
  }
  return;
}
int main() {
  int t = 1;
  do {
    cout << "Case #"<<t++<<": Dilbert should drink beverages in this order:";
    solve();
    cout<<".\n\n";
    adjlist.clear();indeg.clear();
    int ch;
    cin.get();cin.get();
    if((ch=cin.get())==EOF) {
      break;
    }
    else {
      cin.putback(ch);
    }
  }while(true);
  return 0;
}
